Finishing touches
The shipping calculation is
simpler than tax since no rounding is involved in our example. The
shipping rate is simply multiplied by the number of items to determine
the total:
$('tr.shipping td.quantity').text(String(totalQuantity));
var shippingRate = parseFloat($('tr.shipping td.price')
.text().replace(/^[^\d.]*/, ''));
var shipping = totalQuantity * shippingRate;
$('tr.shipping td.cost').text('$' + shipping.toFixed(2));
totalCost += shipping;
We have been keeping track of the grand total as we have gone along, so all we need to do for this last cell is to format totalCost appropriately:
$('tr.total td.cost').text('$' + totalCost.toFixed(2));
Now, we have completely replicated any server-side calculations that would occur so we can safely hide the Recalculate button:
$('#recalculate').hide();
This change once again echoes our progressive enhancement
principle: First, ensure that the page works properly without
JavaScript. Then, use jQuery to perform the same task more elegantly
when possible.